#include <atlbase.h>

#include "Clipboard.h"

//
// http://msdn.microsoft.com/en-us/library/ms649039(VS.85).aspx
//
// Remarks
//
// The clipboard controls the handle that the GetClipboardData function returns, not the application. The application should copy the data immediately. The application must not free the handle nor leave it locked. The application must not use the handle after the EmptyClipboard or CloseClipboard function is called, or after the SetClipboardData function is called with the same clipboard format.
//



CClipboard::CClipboard(void) : _opened(false)
{
	_opened = OpenClipboard(NULL) ? true : false;
}


CClipboard::~CClipboard(void)
{
	if(_opened)
		CloseClipboard();
}

unique_ptr<Bitmap> CClipboard::GetImage() const throw(...)
{
	if(!_opened)
		throw wstring(L"Clipboard is not opened");

	unique_ptr<Bitmap> upbmp;

	HANDLE hHandle = GetClipboardData(CF_BITMAP);
	if(hHandle)
	{
		upbmp.reset(new Bitmap((HBITMAP)hHandle, NULL));
	}
	else
	{
		throw wstring(L"Clipboard does not contain any image");
	}	

	return upbmp;
}

wstring CClipboard::GetText() const throw(...)
{
	if(!_opened)
		throw wstring(L"Clipboard is not opened");

	//TODO
	return wstring();
}
